<template>
	<view>
		<view style="width: 100%;text-align: center; padding-top: 50px; font-size: 40px;">
			在线改密
		</view>
		
		<view class="adddiv2">
			<input v-model="password" class="uni-input addinput" password type="text" placeholder="请输入旧密码" />
		</view>
		
		<view class="adddiv2">
			<input v-model="newPassword" class="uni-input addinput" password type="text" placeholder="请输入新密码" />
		</view>
		
		<view class="adddiv3">
		    <button plain="true" class="addbtn" type="primary" :disabled="log" @click="xiugai">立即修改</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				password:'',
				newPassword:'',
				id: '',
				JSESSIONID:''
			}
		},
		onLoad(id) {
			this.id = id.id
		},
		methods:{
			getCookies() {
			  if (document.cookie.length > 0) {
			    var arr = document.cookie.split("; "); //这里显示的格式需要切割一下自己可输出看下
			    for (var i = 0; i < arr.length; i++) {
			      var arr2 = arr[i].split("="); //再次切割
			      //判断查找相对应的值
			      if (arr2[0] == "JSESSIONID") {
			        this.JSESSIONID = arr2[1]; //保存到保存数据的地方
			      }
			    }
			  }
			},
			xiugai() {
				if(this.ispasswordsame == true) {
					let that = this
					uni.request({
					    url: 'http://101.37.15.60:8080/api/v1/users/resetpwd', 
						method: 'PUT',
					    data: {
					        id: that.id,
							newPassword: that.newPassword,
							password: that.password
					    },
					    header: {
					    	'content-type': 'application/x-www-form-urlencoded'
					    },
						withCredentials: true,
					    success: (res) => {
					        if(res.data.message == "SUCCESS") {
								uni.showToast({
								    title: '修改成功。',
								    duration: 1500
								})
							}else{
								uni.showToast({
									icon:'none',
								    title: '修改失败，请稍后重试。',
								    duration: 1500,
								})
							}
					    }
					});
				}else{
					uni.showToast({
						icon:'none',
					    title: '两次密码不能相同。',
					    duration: 1500
					})
				}
			}
		},
		computed: {
			log() {
				if(!this.password || !this.newPassword) {
					return true
				}else {
					return false
				}
			},
			ispasswordsame() {
				if(this.password === this.newPassword) {
					return false
				}else if(this.password != this.newPassword) {
					return true
				}else if(this.password == "" || this.newPassword == "") {
					return false
				}
			}
		}
	}
</script>

<style scoped>
	.addinput {
		width: 200px;
		height: 40px;
		border-radius: 15px;
		box-shadow: 1px 2px 2px #606266;
	}
	.adddiv {
		display: flex;
		justify-content: center;
		padding: 0 80px 0 80px;
		width: 100%;
		height: 75px;
		margin-top: 50px;
	}
	.adddiv2 {
		display: flex;
		justify-content: center;
		padding: 0 80px 0 80px;
		width: 100%;
		height: 75px;
	}
	.adddiv3 {
		display: flex;
		justify-content: center;
		padding: 0 80px 0 80px;
		width: 100%;
	}
	.addbtn {
		width: 200px;
		border-radius: 20px;
		background-color: #409EFF;
	}
	.adddiv4 {
		display: flex;
		justify-content: center;
	}
	.adddiv5 {
		display: flex;
		justify-content: center;
		padding-top: 25px;
	}
	.addtext {
		width: 100px;
		text-align: center;
	}
	.addsend {
		position: absolute;
		width: 95px;
		height: 40px;
		right: 80px;
		background-color: #FFFFFF;
		border-radius: 15px;
		text-align: center;
		vertical-align: middle;
		font-size: 16px;
	}
</style>
